package com.aspire.moss.portal;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;

import com.aspire.moss.portal.view.AppInfo;
import com.aspire.moss.portal.view.ClipImageView;
import com.aspire.moss.portal.view.DragLayer;
import com.aspire.moss.portal.view.EventCenter;
import com.aspire.moss.portal.view.Workspace;
import com.aspire.moss.portal.view.WorkspaceAdapter;

public class Main extends Activity {

	private static final String TAG = "Main";

	private Context mContext;

	private Workspace workspace;

	ClipImageView clipImageView;
	
	EventCenter eventCenter;
	
	DragLayer dragLayer;
	
	WorkspaceAdapter adapter ;

	public void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "Main : onCreate");
		mContext = this;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Log.d(TAG, "density :" + mContext.getResources().getDisplayMetrics().densityDpi);
		dragLayer = (DragLayer) findViewById(R.id.draglayer);
		workspace = (Workspace) findViewById(R.id.workspace);
		clipImageView = (ClipImageView) findViewById(R.id.clipview);
		workspace.setWorkspaceListener(clipImageView);
		eventCenter = new EventCenter(mContext, workspace ,dragLayer );
		dragLayer.setmCustomTouchEvent(eventCenter);
		HashMap<Integer,LinkedList<AppInfo>> apps = createDatasourre();
		adapter = new WorkspaceAdapter(mContext, apps);
		adapter.setOnClickListener(eventCenter);
		adapter.setOnLongClickListener(eventCenter);
		adapter.setOnDeleteClickListener(eventCenter);
		workspace.setWorkspaceAdapter(adapter);
		adapter.notifyObservers();
		
//		getSharedPreferences(name, mode)
	}
	
	private HashMap<Integer,LinkedList<AppInfo>> createDatasourre(){
		
		HashMap<Integer,LinkedList<AppInfo>> apps =new HashMap<Integer,LinkedList<AppInfo>>();
		LinkedList<AppInfo> page = null;
		Random random = new Random();
		AppInfo info = null;
		for(int i = 0 ;i < 4 ;i++){
			page = new LinkedList<AppInfo>();
			info = new AppInfo(i, 0, 0, getBitmap() , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 0, 1, getBitmap() , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 0, 2, getBitmap() , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 0, 3, getBitmap(), "程序程" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 1, 0, getBitmap() , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 1, 1, getBitmap() , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 1, 2, getBitmap() , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 2, 0, getBitmap() , "程序程" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 2, 1, getBitmap() , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 2, 2, getBitmap() , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 3, 0, getBitmap() , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 3, 1, getBitmap() , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 3, 2, getBitmap() , "程序程序" + random.nextInt(10));
			page.add(info);
			apps.put(i, page);
		}
		return apps;
	}
	
	private Bitmap getBitmap(){
		int id [] = {
				R.drawable.cmbpb,
		        R.drawable.comandroidbrowser,
		        R.drawable.comandroidcalculator2,
		        R.drawable.comandroidemail,
		        R.drawable.comandroidsettings,
		        R.drawable.comandroidstk,
		        R.drawable.comapkinstall,
		        R.drawable.comaspirempy,
		        R.drawable.comdatavizstargate,
		        R.drawable.comdigitalchocolateandroidgotham,
		        R.drawable.comdigitalchocolateandroidtuxedo,
		        R.drawable.comeoemobilenetmarket,
		        R.drawable.comexampleandroidapis,
		        R.drawable.comgoogleandroidappsmaps,
		        R.drawable.comjingdongappmall,
		        R.drawable.comkingreaderframework,
		        R.drawable.comkugouandroid,
		        R.drawable.commobisystemsoffice,
		        R.drawable.comnavbuilderappatlasbook,
		        R.drawable.comnoshufouandroidsu,
		        R.drawable.comqiyivideo,
		        R.drawable.comsemcwebalbummmi,
		        R.drawable.comsinaweibo,
		        R.drawable.comsonyericssonalarm,
		        R.drawable.comsonyericssonandroidsupport,
		        R.drawable.comsonyericssoncalendar,
		        R.drawable.comsonyericssoncamera,
		        R.drawable.comsonyericssoncontacts,
		        R.drawable.comsonyericssonconversations,
		        R.drawable.comsonyericssondialer,
		        R.drawable.comsonyericssonfmradio,
		        R.drawable.comsonyericssonmusic,
		        R.drawable.comsonyericssonnotes,
		        R.drawable.comsonyericssonplaynowstoreandroid,
		        R.drawable.comsonyericssonsetupwizard,
		        R.drawable.comsonyericssonstopwatch,
		        R.drawable.comsonyericssontimer,
		        R.drawable.comsonyericssontimescape,
		        R.drawable.comsonyericssontrackid3client,
		        R.drawable.comsonyericssonvideo,
		        R.drawable.comspeedsoftwarerootexplorer,
		        R.drawable.comspritemobilebackupsemc2,
		        R.drawable.comtencentandroidqqdownloader,
		        R.drawable.comtencentmtt,
		        R.drawable.comtencentqq,
		        R.drawable.comtencentqqlauncher,
		        R.drawable.comtencentqqphonebook,
		        R.drawable.comtencentqqpimsecure,
		        R.drawable.comtencentqqpinyin,
		        R.drawable.comtencentwblog,
		        R.drawable.comtigerknows,
		        R.drawable.comtouchnoteandroid,
		        R.drawable.comucbrowser,
		        R.drawable.boxy_btn
		};
		Random random = new Random();
		int idIndex = random.nextInt(id.length);
		Drawable  drawable = getResources().getDrawable(id[idIndex]);
		return ((BitmapDrawable)drawable ).getBitmap();
	}

//	private Bitmap excuteBitmap(Drawable drawable) {
//		Drawable temp = drawable;
//		while(temp==null){
//			temp = getBitmap();
//		}
//		BitmapDrawable bitmapDrawable =(BitmapDrawable) temp;
//		Bitmap bitmap = bitmapDrawable.getBitmap();
//		
//		int icon = (int) mContext.getResources().getDimension(R.dimen.icon);
//		Bitmap app = Bitmap.createBitmap(icon, icon, Config.ARGB_8888);
//		
//		android.graphics.Matrix matrix = new Matrix();
//		matrix.preScale(icon, icon);
//		Canvas canvas = new Canvas(app);
//		RectF rect = new RectF(0, 0, icon, icon);
//		Paint paint = new Paint();
//		paint.setFlags(Paint.FILTER_BITMAP_FLAG);
//		canvas.drawRect(rect , paint );
//		canvas.drawBitmap(bitmap, matrix, null);
//		canvas.save();
//		return app;
//	}
	
	@Override
	public void onBackPressed() {
		if(workspace.isLockWorkspace()){
			workspace.unlockWorkspace();
		}else{
			super.onBackPressed();
			Process.killProcess(Process.myPid());
		}
		
		adapter.printInfo();
	}
	
}
